package edu.scau.mis.pos.service.impl;

import edu.scau.mis.pos.domain.Category;

import edu.scau.mis.pos.mapper.CategoryMapper;
import edu.scau.mis.pos.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 查询所有类别树形结构--getCategoryTree
     * @return
     */
    @Override
    public List<Category> getCategoryTree() {
        // 查询所有顶级类别（parent_id 为 null）
        List<Category> topLevelCategories = categoryMapper.findAllParent();
        // 递归构建树形结构
        List<Category> categoryTree =  topLevelCategories.stream()
                .peek(category -> category.setChildren(findChildren(category.getCategoryId())))
                .collect(Collectors.toList());
        return categoryTree;
    }
    @Override
    public List<Category> getTopCategory(){
        // 查询所有顶级类别（parent_id 为 null）
        List<Category> topLevelCategories = categoryMapper.findAllParent();
        return topLevelCategories;
    }
    // 递归查找子类别
    @Override
    public List<Category> findChildren(Long parentId) {
        List<Category> children = categoryMapper.findByParentId(parentId);
        return children.stream()
                .peek(child -> child.setChildren(findChildren(child.getCategoryId())))
                .collect(Collectors.toList());
    }

    /**
     *  根据ID获取所有类别的树形结构
     * @param categoryId
     * @return
     */

    public Category getCategoryById(Long categoryId) {
        Category category = categoryMapper.findById(categoryId); // 假设有一个根据ID查询的方法
        if (category != null) {
            category.setChildren(findChildren(category.getCategoryId()));
        }
        return category;
    }

    /**
     * 根据类别名称获取所有类别的树形结构
     * @param categoryName
     * @return
     */
    @Override
    public List<Category> getCategoryByName(String categoryName) {
        List<Category> categories = categoryMapper.findByName(categoryName); // 假设有一个根据名臣、称查询的方法
        // 递归构建树形结构
        List<Category> categoryTree =  categories.stream()
                .peek(category -> category.setChildren(findChildren(category.getCategoryId())))
                .collect(Collectors.toList());
        return categoryTree;

    }



    @Override
    public List<Category> getTopLevelCategory() {
        // 查询所有顶级类别（parent_id 为 null）
        List<Category> topLevelCategories = categoryMapper.findAllParent();
        // 递归构建树形结构
        List<Category> categoryTree =  topLevelCategories.stream()
                .peek(category -> category.setChildren(categoryMapper.findByParentId(category.getCategoryId())))
                .collect(Collectors.toList());
        return categoryTree;
    }


    @Override
    public int deleteCategoryById(Long categoryId) {
        return categoryMapper.deleteCateoryById(categoryId);
    }

    @Override
    public int deleteCategoryByIds(Long[] categoryIds) {
        return categoryMapper.deleteCateoryByIds(categoryIds);
    }





    @Override
    public int insertCategory(Category category) {
        category.setUpdateTime(new Date());
         //如果 parentId 不为空，则查询对应的 父类名称
        if (category.getParentId() != null) {
            String parentName = categoryMapper.findById(category.getParentId()).getCategoryName();
            category.setDescription(parentName.concat(category.getCategoryName()));
        }else
            category.setDescription(category.getCategoryName());

        // 执行插入操作
        return  categoryMapper.insertCategory(category);
    }
    @Override
    public int updateCategory(Category category) {
        category.setUpdateTime(new Date());
        // 如果 parentName 不为空，则查询对应的 parentId
//        if (category.getParentName() != null) {
//            Long parentId = categoryMapper.selectParentId(category.getParentName());
//            category.setParentId(parentId);
//        }
        System.out.println(new Date());
        return categoryMapper.updateCategory(category);
    }



    @Override
    public List<Category> selectCategoryList(Category category) {
        List<Category> categoryList = categoryMapper.selectCategoryList(category);
//        System.out.println(categoryList);
//        // 递归构建树形结构
//        List<Category> categoryTree =  categoryList.stream()
//                .peek(categories -> categories.setChildren(categoryMapper.findByParentId(categories.getCategoryId())))
//                .collect(Collectors.toList());
//        return categoryTree;
            return categoryList;
    }




}

